﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HotelManagement._04.OBJECT;
using HotelManagement._02.BUS;
using HotelManagement;

namespace HotelManagement._01.FORM
{
    public partial class F23_DestroyRevervation : Form
    {

        Reservation m_Reservation;
        ReservationBUS m_ReservationBUS;

        RoomStatusBUS m_RoomStatusBUS;
        CheckOutBUS m_CheckOutBUS;

        DestroyRevervation m_DestroyRevervation;
        DestroyRevercationBUS m_DestroyRevercationBUS;

        CheckInBUS m_CheckInBUS;
        CheckInInfoBUS m_CheckInInfoBUS;
        public F23_DestroyRevervation()
        {
            InitializeComponent();
            init();
            //loadUser();
            loadGrid();
            if (AssetsGS.instance.RoleUser == "User")
            {
                m_cmd_delete.Enabled = false;
            }

        }
        public void init()
        {
            m_ReservationBUS = new ReservationBUS();
            m_Reservation = new Reservation();
            m_RoomStatusBUS = new RoomStatusBUS();
            m_CheckOutBUS = new CheckOutBUS();
            m_DestroyRevervation = new DestroyRevervation();
            m_DestroyRevercationBUS = new DestroyRevercationBUS();
            m_CheckInBUS = new CheckInBUS();
            m_CheckInInfoBUS = new CheckInInfoBUS();
        }
        public void loadGrid()
        {
            m_dgv_reservation.DataSource = m_CheckInBUS.ViewReservation(DateTime.Now);
            m_dgv_RoomIsCanceled.DataSource = m_CheckInInfoBUS.ViewRoomIsCanceled();
        }

        public void loadItem(Reservation v)
        {
            m_txt_RoomID.Text = v.RoomID;
            m_txt_CustomerName.Text = v.CustomerName;
            m_date_arrivalDate.Value = v.ArrivalDate;
            m_date_departureDate.Value = v.DepartureDate;
            //m_date_reservationDate.Value = v.ReservationDate;
        }

        private void m_dgv_reservation_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            m_Reservation.ReservationID = Convert.ToInt32(m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["ReservationID"].Value.ToString());
            m_Reservation.RoomID = m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["RoomID"].Value.ToString();
            //m_Reservation.UserID = Convert.ToInt32(m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["UserID"].Value.ToString());
            //m_Reservation.ReservationDate = Convert.ToDateTime(m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["ReservationDate"].Value.ToString());
            m_Reservation.ArrivalDate = Convert.ToDateTime(m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["ArrivalDate"].Value.ToString());
            m_Reservation.CustomerID = Convert.ToInt32(m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["CustomerID"].Value.ToString());
            m_Reservation.DepartureDate = Convert.ToDateTime(m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["DepartureDate"].Value.ToString());
            m_Reservation.CustomerName = m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["CustomerName"].Value.ToString();
            m_Reservation.Note = m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["Note"].Value.ToString();
            m_DestroyRevervation.DepositCost = Convert.ToInt32(m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["DepositCost"].Value.ToString());
            m_DestroyRevervation.Fines = 0;
            if ((m_Reservation.ArrivalDate.DayOfYear - DateTime.Now.DayOfYear) <= 1)
            {
                DataTable v_dtCost = m_CheckOutBUS.GetCostByRoomID(m_Reservation.RoomID);
                m_DestroyRevervation.Fines = int.Parse(v_dtCost.Rows[0][0].ToString());
                
            }
            m_txt_Fines.Text = m_DestroyRevervation.Fines.ToString();
            loadItem(m_Reservation);
        }

        private void m_cmd_delete_Click(object sender, EventArgs e)
        {
            if (m_Reservation.ReservationID == 0)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi nào nào !");
            }
            else
            {
                string Status;
                DialogResult OK;
                OK = MessageBox.Show("Bạn muốn Hủy đặt phòng đã chọn ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (OK == DialogResult.Yes)
                {

                    DataTable v_dt = m_RoomStatusBUS.ViewOnlyRoom(m_Reservation.RoomID);
                    if (v_dt.Rows.Count > 0)
                    {
                        Status = "Booked";
                    }
                    else
                    {
                        Status = "Empty";
                    }

                    if (m_ReservationBUS.tblReservation_DeleteRow(m_Reservation.ReservationID) && m_ReservationBUS.tblRoom_ChangeStatus(m_Reservation.RoomID, Status))
                    {
                        m_DestroyRevervation.CheckOutID = 2;
                        m_DestroyRevervation.CustomerID = m_Reservation.CustomerID;
                        m_DestroyRevervation.RoomID = m_Reservation.RoomID;
                        m_DestroyRevervation.Note = "UnPaid";
                        m_DestroyRevervation.CheckOutDate = DateTime.Now;
                        
                        if (m_DestroyRevervation.Fines > 0)
                        {
                            if (m_DestroyRevercationBUS.InsertCheckInForDestroyRevervation(m_DestroyRevervation))
                            {
                                MessageBox.Show("Thành công !");
                                loadGrid();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Thành công !");
                            loadGrid();
                        }
                    }
                    else MessageBox.Show("Có lỗi! Không cập nhật được.");
                }
            }
        }
               
        
        private void m_cmd_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        

    }
}